package day20171016;
//功能：演示各种语句综合应用

/**
 * Created by 阿阿阿阿豪丶 on 2017/10/16.
 */

import com.sun.org.apache.xpath.internal.SourceTree;

import java.io.*;

public class Practice {
    public static void main(String[] args) {
        char ch = '5';                              //此处赋初始值
        int i, m, n, x, y;
        long sum, fact, p;
        while (ch != '0') {
            System.out.println("1.输出1+2+...+n的结果");
            System.out.println("2.输出1*2*...*n的结果");
            System.out.println("3.输出两个随机整数的最大值");
            System.out.println("4.求使得1+2+..+n的值大于p的最小n值");
            System.out.println("5.求m和n的最大公约数和最小公倍数");
            System.out.println("0.退出系统");
            System.out.println("选择[0.1.2.3.4.5]");
            try {
                ch = (char) System.in.read();                   //键盘键入一个字符
            } catch (IOException e) {
                //此处写产生输入错误的处理代码
                while (ch != '1' || ch != '2' || ch != '3' || ch != '4' || ch != '5') {
                    System.out.println("输入有误");
                }                //输出异常信息
            }
            switch (ch) {
                case '1':
                    n = (int) (Math.random() * 100);
                    System.out.println("sum(1+.." + n + ")=" + sum(n));
                    break;
                case '2':
                    n = (int) (Math.random() * 20);
                    System.out.println(n + "!=" + fact(n));
                    break;
                case '3':
                    x = (int) (Math.random() * 100);
                    y = (int) (Math.random() * 100);
                    System.out.println("max(" + x + "," + y + ")=" + max(x, y));
                    break;
                case '4':
                    p = 100 + (int) (Math.random() * 10000);
                    System.out.println("(1+2+..+n)>=" + p + "的最小n=" + minN(p));
                    break;
                case '5':
                    m = 10 + (int) (Math.random() * 1000);
                    n = 10 + (int) (Math.random() * 1000);
                    System.out.println(m + "和" + n + "的最大公约数=" + max1(n, m));
                    System.out.println(m + "和" + m + "的最小公倍数=" + m * n / max1(n, m));
                    break;
                case '0':
                    System.out.println("退出系统");
                    break;
            }
        }
    }

    static long sum(int n) {
        long sum1 = 0;
        int i;
        for (i = 0; i <= n; i++) sum1 = sum1 + i;
        return sum1;
    }

    static long fact(int n) {
        long fact1 = 1;
        int i;
        for (i = 0; i <= n; i++) fact1 = fact1 + i;
        return fact1;
    }

    static int max(int x, int y) {
        return x > y ? x : y;
    }

    static int minN(long m) {
        long sum = 0;
        int n = 0;
        while (sum < m) {
            sum = sum + n;
            n = n + 1;
        }
        return n;
    }

    static int max1(int n, int m) {
        int temp, r;
        if (n < m) {
            temp = n;
            n = m;
            m = temp;
        }
        while (m != 0) {
            r = n % m;
            n = m;
            m = r;
        }
        return n;
    }

}
